package 代码记录.Bit48天集训;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**答案已订正
 * 1.字符串中找出连续最长的数字串
 * 2.数组中出现次数超过一半的数字
 * @author zx
 * @create 2022-04-21 14:54
 */
public class day_03 {
    /**
     * 算法思想：用max 表示经过的数字长度最大值, count表示数字计数器,当为字母时重置为0
     * end表示数字尾部,每次满足数字时,对max进行判断,当max小于于count时,更新max和end
     *
     * 别人写的好的代码：String.substring()自己还得在多用用！
     */
    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.nextLine();
            int max = 0, count = 0, end = 0;
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                    count++;
                    if (max < count) {
                        max = count;
                        end = i;
                    }
                } else {
                    count = 0;
                }
            }
            System.out.println(str.substring(end - max + 1, end + 1));
        }
    }
    /**
     * @param args 字符串中找出连续最长的数字串
     */
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] arr = str.toCharArray();
        int maxLength = 0;
        int len = 0;
        int right = 0;
        for(int i = 0;i < arr.length;i++){
            if(arr[i] - '0' >= 0 && arr[i] - '0' <= 9){
                //此时都是数字
                len++;
                if(maxLength < len){
                    maxLength = len;
                    right = i;
                }
            }else{
                //遇到字母了
                len = 0;
            }
        }
        String str1 = "";
        for(int i = 0;i < arr.length && maxLength > 0;i++){
            if(i > right - maxLength){
                str1 += arr[i];
                maxLength--;
            }
        }
        System.out.println(str1);
    }

    /**
     * @return 数组中出现次数超过一半的数字
     * 时间复杂度O(N)
     */
    public int MoreThanHalfNum_Solution(int[] array) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < array.length;i++){
            if(map.containsKey(array[i])){
                map.put(array[i],map.get(array[i]) + 1);
            }else{
                map.put(array[i],1);
            }
        }
        int maxValue = Integer.MIN_VALUE;
        int max = 0;
        for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            if(maxValue < entry.getValue()){
                maxValue = entry.getValue();
                max = entry.getKey();
            }
        }
        return max;
    }

    /**
     * @return 时间复杂度O(nlogn)
     */
    public int MoreThanHalfNum_Solution2(int [] array) {
        Arrays.sort(array);
        return array[array.length / 2];
    }
}

